package com.yanqu.road.server.cmd.activity.stronghold.wild;

import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.entity.activity.stronghold.config.StrongholdTimeConfig;
import com.yanqu.road.entity.enums.activity.stronghold.eStrongholdTimeType;
import com.yanqu.road.entity.enums.eLogMoneyType;
import com.yanqu.road.entity.enums.ePlayerAttrType;
import com.yanqu.road.logic.config.GameConfig;
import com.yanqu.road.logic.helper.UnionHelper;
import com.yanqu.road.logic.pb.PlayerBasePb;
import com.yanqu.road.logic.pb.UnionBasePb;
import com.yanqu.road.pb.activity.StrongholdWarCrossProto;
import com.yanqu.road.pb.activity.StrongholdWarProto;
import com.yanqu.road.server.GameServer;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.player.AttributeModule;
import com.yanqu.road.server.gameplayer.module.player.CurrencyModule;
import com.yanqu.road.server.manger.activity.strongholdwar.StrongholdActivityMgr;
import com.yanqu.road.server.manger.activity.strongholdwar.StrongholdWarMgr;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.manger.player.UserMgr;
import com.yanqu.road.server.manger.union.CrossUnionMgr;
import com.yanqu.road.server.manger.union.UnionMgr;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.ClientProtocol;
import com.yanqu.road.server.protocol.CrossProtocol;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.GameProtocol;
import com.yanqu.road.utils.property.Property;
import com.yanqu.road.utils.string.StringUtils;

import java.math.BigInteger;
import java.util.List;

@Cmd(code = GameProtocol.S_STRONGHOLD_WAR_SILVER_DONATE_REQ, desc = "银两捐献")
public class StrongholdDonateSilverCmd extends AbstractCommand {
    private BigInteger num;

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        StrongholdWarProto.StrongholdSilverDonateReqMsg req = StrongholdWarProto.StrongholdSilverDonateReqMsg.parseFrom(packet.getBytes());
        num = new BigInteger(req.getNum());
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
        ActivityInfo activityInfo = StrongholdActivityMgr.getActivityInfo();
        if (activityInfo == null) {
            ret = GameErrorCode.E_STRONGHOLD_NOT_FOUND;
            return;
        }
        String unionUid = player.getUserInfo().getUnionUid();

        if (StringUtils.isNullOrEmpty(unionUid)) {
            ret = GameErrorCode.E_STRONGHOLD_NO_UNION;
            return;
        }

        if (!StrongholdActivityMgr.isDonateSliverTime()) {
            ret = GameErrorCode.E_STRONGHOLD_DONATE_SILVER_CLOSE;
            return;
        }

        if (StrongholdActivityMgr.isAfterWildTime()) {
            List<StrongholdTimeConfig> configList = StrongholdActivityMgr.getStrongholdTimeConfig();
            for (StrongholdTimeConfig config : configList) {
                if (config.getType() == eStrongholdTimeType.REST.getValue()) {
                    long now = System.currentTimeMillis() / 1000;
                    if (activityInfo.getBeginTime() + config.getBeginTime() * 3600 + 300 > now) {
                        ret = GameErrorCode.E_STRONGHOLD_CALC_RANK;
                        return;
                    }
                }
            }
            if (!StrongholdWarMgr.hasQualification(activityInfo.getActivityId(), unionUid)) {
                ret = GameErrorCode.E_STRONGHOLD_NO_QUALIFICATION;
                return;
            }
        }

        int activityId = activityInfo.getActivityId();
        if (!player.getModule(CurrencyModule.class).removeCurrency(new Property(GameConfig.GAME_MONEY_SILVER, num), eLogMoneyType.Activity, eLogMoneyType.Activity)) {
            ret = GameErrorCode.E_BAG_SILVER_NO_ENOUGH;
            return;
        }
        player.getModule(AttributeModule.class).syncAttribute(ePlayerAttrType.Silver);
        //直接发到跨服捐献吧
        StrongholdWarCrossProto.StrongholdCrossDonateSilverReqMsg.Builder msg = StrongholdWarCrossProto.StrongholdCrossDonateSilverReqMsg.newBuilder();
        msg.setActivityId(activityId);
        msg.setUserId(player.getUserId());
        msg.setNum(num.toString());
        msg.setUnionId(unionUid);
        msg.setUserInfo(PlayerBasePb.parsePlayerBaseTempMsg(UserMgr.getUserBaseInfo(player.getUserId(), GameServer.getInstance().getServerId())));
        msg.setUnionInfo(UnionBasePb.parseUnionBaseTempMsg(CrossUnionMgr.parseUnionBaseInfo(unionUid)));
        player.sendUnionActivityGroupPacket(CrossProtocol.C_CROSS_STRONGHOLD_DONATE_SILVER, msg, msg.getActivityId());
        //StrongholdWarMgr.donateSilver(activityId, unionUid, player.getUserId(), num);
    }

    @Override
    public void sendData(GamePlayer player) {
        StrongholdWarProto.StrongholdSilverDonateRespMsg.Builder msg = StrongholdWarProto.StrongholdSilverDonateRespMsg.newBuilder();
        msg.setRet(ret);
        player.sendPacket(ClientProtocol.U_STRONGHOLD_WAR_SILVER_DONATE_REQ, msg);
    }
}
